/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.context.job;

import com.context.AppContext;
import com.model.service.DataXmlCampoService;
import java.io.IOException;
import java.text.ParseException;
import java.util.logging.Level;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.CronTriggerImpl;

/**
 *
 * @author Rdelacruz
 */
public class CargaAutomaticaJob  implements Job {
    
    private String horas;
    private String patron;
    private Scheduler scheduler;
    
    
        
    private static final String HORAS = "12,0";//cada dia a las 12 y 00 horas
    //public static final String horas = "7,13,19";//cada dia a las 7am, 1pm y 7pm
        
    public CargaAutomaticaJob() {
        
    }

    @Override
    public void execute(JobExecutionContext jec) throws JobExecutionException {        
        try {
            AppContext app = AppContext.getInstancia();            
            //La carga automática solo se realiza si el sistema está en producción.
            //Esto se hace para que el sistema en desarrollo no realice una carga automática, 
            //al mismo tiempo que el sistema en producción
            if(app.getAplicacionProperties().isEnProduccion()) {            
                DataXmlCampoService dataXmlCampoService = app.getModelContext().getDataXmlCampoService();            
                dataXmlCampoService.cargarDataXML_Automatica(null,null);
            }
        } catch (Exception ex) {
            java.util.logging.Logger.getLogger(CargaAutomaticaJob.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    
    
    public void iniciar() 
            throws ParseException, SchedulerException, IOException {
                
        horas = HORAS;
        patron = "0 0 "+horas+" * * ?";
        //patron = "0/30 * * * * ?"; //cada 30 segundos
                       
                
        JobDetail jd = new JobDetailImpl( 
            "Tarea1", 
            Scheduler.DEFAULT_GROUP,
                CargaAutomaticaJob.class);             
        
        CronTriggerImpl ct = new CronTriggerImpl(
            "Trigger1", 
            Scheduler.DEFAULT_GROUP, 
            patron);        
        
        scheduler = StdSchedulerFactory.getDefaultScheduler();
        scheduler.scheduleJob(jd, ct);
        scheduler.start();                
    }
    
     public void detener() throws SchedulerException {       
        scheduler.shutdown();
    }
   
    
    public String getPatron() {
        return patron;
    }

    public Scheduler getScheduler() {
        return scheduler;
    }

    public String getHoras() {
        return horas;
    }
      
    
    
}
